<?php
/**
 * 新闻系统  只考虑分类绑定模版 频道页 列表页 内容页 不支持在线修改  这里为前端
 *@author wolf  [Email: 116311316]
 *@since 2010-11-28
 *@version 1.0
 */
class NewsController extends NodeController {
	static $newService;
	static $cateService;
	
	public function c() {
		//这里穿进去的是$_GET 参数
		//执行代码
		$cid = $_GET ['cid'];
		$p = isset ( $_GET ['p'] ) ? $_GET ['p'] : 1;
		$sort = isset ( $_GET ['sort'] ) ? $_GET ['sort'] : NULL;
		//停止xhprof
		$nInfo = self::getCateService ()->getCateTemp ( $cid, $p, 'temp_list' ); //调用内容
		

		$num = isset ( $_GET ['num'] ) ? $_GET ['num'] : 0;
		$newslist = self::getNewsService ()->listing ( $cid, $p, $sort, $num );
		
		//导入插件
		self::getPluginService ( 'list' )->run ();
		
		$rs = array_merge ( $nInfo, $newslist );
		$this->view ()->assign ( 'news', $rs ['newslist'] );
		$this->view ()->assign ( 'totalnum', $rs ['totalnum'] );
		$this->view ()->assign ( 'num', $rs ['page'] );
		$this->view ()->assign ( 'module', $rs ['module'] );
		$this->view ()->assign ( 'category', $rs ['cate'] );
		$content = $this->view ()->fetch ( $rs ['tempname'] );
		echo  self::getNewsService ()->parseImagePath ( $content );
	
	}
	
	/**
	 * 专题页
	 * 用来替换 cid=1&type=temp_index
	 */
	public function i() {
		$cid = $_GET ['cid'];
		
		$nInfo = self::getCateService ()->getCateTemp ( $cid, null, 'temp_index' ); //调用内容
		$cate = $this->view ()->assign ( 'category', $nInfo ['cate'] );
		$this->view ()->display ( $nInfo ['tempname'] );
	}
	/**
	 * 根据内容的分类 绑定所需模板
	 */
	public function v($vid = NULL) {
		
		$id = $vid != NULL ? $vid : trim ( $_GET ['id'] );
		
		if (empty ( $id )) {
			header ( "HTTP/1.1 404 Not Found" );
			/* 内容不存在 */
		}
		
		//如果链接自带模板 覆盖上方
		$temp = isset ( $_GET ['mb'] ) ? $_GET ['mb'] : null;
		$con = self::getNewsService ()->getCon ( $id );
		$news = self::getCateService ()->setConTemp ( $con ['content'], $temp );
		// 设置绑定模板
		if (! $news) {
			header ( "HTTP/1.1 404 Not Found" );
		}
		$this->view ()->assign ( 'content', $news ['content'] );
		
		// 导入ajax评论
		if (isset ( $_GET ['mid'] ) && $_GET ['mid'] > 0) {
			$mid = $_GET ['mid'];
			$this->view ()->assign ( 'commentid', $mid );
		}
		
		//导入插件
		$_GET ['id'] = $id;
		$_GET ['cid'] = $news ['content'] ['cid'];
		self::getPluginService ( 'content' )->run ();
		
		$content = $this->view ()->fetch ( $news ['tempname'] );
		$content = self::getNewsService ()->parseImagePath ( $content );
		
		//解析content
		if ($news ['content'] ['mid'] < 2) {
			$content = self::getNewsService ()->parseImageAlt ( $content, $news ['content'] ['title'] );
		}
		
		if ($vid != null) {
			return array ('content' => $content, 'title' => $news ['content'] ['title'] );
		}
		
		echo $content;
	
	}
	
	/**
	 * 搜索页面
	 */
	public function search() {
		/* 增加扩展字段 也就是分类搜索 */
		if (! isset ( $_REQUEST ['key'] ) || ! isset ( $_REQUEST ['value'] )) {
			return;
		}
		
		$newslist = self::getNewsService ()->search ( $_REQUEST ['key'], $_REQUEST ['value'] );
		
		if ($_REQUEST ['datatype'] == 'json') {
			$this->sendNotice ( "success", $newslist, true );
		}
		$this->view ()->assign ( 'news', $newslist );
		$this->view ()->display ( "mysql:$_REQUEST[template]" );
	}
	
	/**
	 * 前端更新评论数接口
	 */
	public function views() {
		if (! isset ( $_GET ['id'] ))
			return;
		
		$rs = self::getNewsService ()->addViews ( trim ( $_GET ['id'] ) );
		$this->sendNotice ( self::SUCEESS, $rs, true );
	
	}
	
	/*批量处理html*/
	public function batch() {
		
		if (! isset ( $_GET ['cid'] ) || ! isset ( $_GET ['mod'] )) {
			echo "no cid!";
			return;
		}
		
		set_time_limit ( 0 );
		$width = 800; //显示的进度条长度，单位 px
		$progress = 0; //当前长度
		//其他程序可以删除这个
		$starttime = $this->microtime_float ();
		$cid = $_GET ['cid'];
		$mod = $_GET ['mod'];
		//获取所有分类
		$ids = self::getNewsService ()->newsModule->_base->getSonCate ( $cid );
		
		$this->view ()->display ( "file:news/batch.tpl" );
		
		flush (); //输出
		

		//批量生成内容
		if ($mod == "v") {
			
			$rs = NewsModel::instance ()->getNewsIdByIds ( $ids );
			$total = count ( $rs ); //总共需要操作的记录数
			$pix = round ( $width / $total, 2 );
			flush ();
			
			foreach ( $rs as $k => $v ) {
				
				$content = $this->v ( $v ['id'] );
				self::getNewsService ()->html ( null, $v ['id'], $content ['content'], "V", $v ['date'] );
				$content = null;
				$pr = round ( $k / $total, 2 ) * 100;
				echo $this->process ( $k, $progress, $pr, $starttime );
				
				flush ();
				
				$progress += $pix;
			
			}
		}
		
		//批量生成分页 暂不支持递归
		if ($mod == "c" && $_GET ['s'] == "none") {
			$this->bathOneCid ( $cid, $starttime );
			return;
		}
		
		//批量生成分页  支持递归
		if ($mod == "c" && $_GET ['s'] == "all") {
			$son = explode ( ",", $ids ); //所有子类包括自己
			foreach ( $son as $v ) {
				$this->bathOneCid ( $v, $starttime );
			
			}
			return;
		}
	
	}
	
	/**
	 * 处理单个分类 静态
	 * @return int pagenum
	 * */
	private function bathOneCid($cid, $startime) {
		$pagenum = 0;
		while ( true ) {
			$pagenum ++;
			$rs = $this->cHtml ( $cid, $pagenum ); //超出分页数后跳出
			if (! $rs) {
				break;
			}
			echo $this->process ( $pagenum, 100, 100, $startime );
			flush ();
		
		}
		return $pagenum - 1;
	}
	/**
	 *
	 * Enter description here ...
	 * @param unknown_type $current
	 * @param unknown_type $proecss
	 * @param unknown_type $pr
	 * @param unknown_type $start
	 */
	private function process($current, $proecss, $pr, $starttime) {
		$mem = round ( memory_get_usage () / (1024 * 1024), 2 );
		$costtime = round ( ($this->microtime_float () - $starttime), 2 );
		return $string = "<script>updateProgress(\"do $current mem: $mem Mb  cost:  $costtime s\", $proecss,$pr);</script>";
	}
	
	/**
	 * 分类静态方法 之前ob_start内存溢出 是因为多了缓存id
	 * */
	public function cHtml($cid, $p) {
		
		$nInfo = self::getCateService ()->getCateTemp ( $cid, $p, 'temp_list' ); //调用内容
		

		$rs = self::getNewsService ()->newsModule->listing ( $cid, $p, 'id', 0 );
		
		if (! $rs) {
			return false;
		}
		
		//导入插件
		self::getPluginService ( 'list' )->run ();
		$cate = $this->view ()->assign ( 'category', $nInfo ['cate'] );
		
		$this->view ()->assign ( 'news', $rs ['newslist'] );
		$this->view ()->assign ( 'totalnum', $rs ['totalnum'] );
		$this->view ()->assign ( 'num', $rs ['page'] );
		$this->view ()->assign ( 'module', $rs ['module'] );
		
		$content = $this->view ()->fetch ( $nInfo ['tempname'] );
		
		if ($nInfo ['cacheid'] == 1) {
			$file = 'index.html';
			file_put_contents ( ROOT . DIRECTORY_SEPARATOR . $file, $content );
			exit ();
		}
		
		$action = strtoupper ( $this->_actionName );
		if ($action != "I") {
			$action = "C";
		}
		
		$cid = $p == 0 ? $cid : $cid . 'p' . $p;
		
		$content = self::getNewsService ()->parseImagePath ( $content );
		self::getNewsService ()->html ( $cid, $nInfo ['cacheid'], $content, $action );
		return true;
	
	}
	
	protected function microtime_float() {
		list ( $usec, $sec ) = explode ( " ", microtime () );
		return (( float ) $usec + ( float ) $sec);
	}
	
	public static function getCateService() {
		if (self::$cateService == null) {
			self::$cateService = new CateService ();
		}
		return self::$cateService;
	}
	
	/**
	 * 获取news服务类
	 * Enter description here ...
	 */
	public static function getNewsService() {
		if (self::$newService == null) {
			self::$newService = new NewsService ( $_REQUEST ['cid'], $_REQUEST ['mid'] );
		}
		return self::$newService;
	}

}